package com.richapp.launcher.serialport.gateway.command;

import android.util.Log;

import java.util.LinkedList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jack.util.string.StringUtil;
import com.richapp.launcher.database.system.dao.BoxModelDao;
import com.richapp.launcher.database.system.dao.DeviceModelDao;
import com.richapp.launcher.database.system.dao.DeviceModelHistoryDao;
import com.richapp.launcher.database.system.mode.BoxModel;
import com.richapp.launcher.database.system.mode.DeviceModel;
import com.richapp.launcher.net.box.tcp.server.BoxServer;
import com.richapp.launcher.net.message.MsgConstant;
import com.richapp.launcher.net.mobile.message.DeviceStatusReply;
import com.richapp.launcher.net.mobile.server.UserServer;
import com.richapp.launcher.net.remote.RemoteClient;
import com.richapp.launcher.serialport.gateway.ControlInstructionMessage;
import com.richapp.launcher.serialport.gateway.GatewaytConstant;
import com.richapp.launcher.serialport.gateway.GatewayServer;

public class QueryCmd extends BasicCommand {
	
	private List<DeviceModel> list = null;
	private static QueryCmd queryCmd = null;
	String deviceStatus="00";
	String deviceStatus2="00";
	String deviceStatus3="00";
	private QueryCmd() {
		list = new LinkedList<DeviceModel>();
	}
	
	public static QueryCmd getInstance(){
		if(queryCmd == null){
			queryCmd= new QueryCmd();
		}
		return queryCmd;
	}
    
	public void query(String boxCpuId,String deviceId, String deviceNumber,String way) {
		this.ID = deviceId;
		this.Number = deviceNumber;
		this.Query = "35";
        this.Way = way;
		ControlInstructionMessage cim = new ControlInstructionMessage(boxCpuId,deviceId, deviceNumber,way);
		cim.setHexInstruction(combinationCMD());
		cim.setInstructionFlag(GatewaytConstant.INSTRUCTION_FLAG_QUERY);
		GatewayServer.getInstance().writeMessage(cim);
	}

	public void readDevice(String boxCpuId,String deviceId, String deviceNumber,String way) {
		this.ID = deviceId;
		this.Number = deviceNumber;
		this.Read = "46";
        this.Way = way;
		ControlInstructionMessage cim = new ControlInstructionMessage(boxCpuId,deviceId, deviceNumber,way);
		cim.setHexInstruction(combinationCMD());
		cim.setInstructionFlag(GatewaytConstant.INSTRUCTION_FLAG_QUERY);
		GatewayServer.getInstance().writeMessage(cim);
	}
	
	//设置设备校验时间(窗帘)
	public void setDeviceCheckTime(String boxCpuId,String deviceId, String deviceNumber,String way,int time){
		this.ID = deviceId;
		this.Number = deviceNumber;
		this.Read = "46";
        this.Way = way;
		ControlInstructionMessage cim = new ControlInstructionMessage(boxCpuId,deviceId, deviceNumber,way);
		cim.setHexInstruction(combinationCheckTimeCMD(time));
		cim.setInstructionFlag(GatewaytConstant.INSTRUCTION_FLAG_SET_CHECK_TIME);
		GatewayServer.getInstance().writeMessage(cim);
	}

	@Override
	public void analyticalCmd(String upStreamCmd) {
		String deviceId = upStreamCmd.substring(8, 10);
		String Number = upStreamCmd.substring(10, 12);
		String way = upStreamCmd.substring(12, 14);
		 deviceStatus = upStreamCmd.substring(14, 16);


		String way2 = null;
		String way3 = null;
		String way4 = null;


//		String deviceStatus2 = null;
//		String deviceStatus3 = null;
		String deviceStatus4 = null;

		String ifAnyone = "false";

        if(GatewaytConstant.getMiltiWayMap().containsKey(deviceId)){
			ifAnyone = upStreamCmd.substring(20, 22);

			if(GatewaytConstant.ID_TYPE_ONE_CASE_NORMAL_SWITCH_STR.equals(deviceId)){


			}else if(GatewaytConstant.ID_TYPE_TWO_CASE_NORMAL_SWITCH_STR.equals(deviceId)){
				way2 = upStreamCmd.substring(24, 26);
				deviceStatus2 = upStreamCmd.substring(26, 28);

			}else if(GatewaytConstant.ID_TYPE_THREE_CASE_NORMAL_SWITCH_STR.equals(deviceId)){
				way2 = upStreamCmd.substring(24, 26);
				deviceStatus2 = upStreamCmd.substring(26, 28);
				way3 = upStreamCmd.substring(28, 30);
				deviceStatus3 = upStreamCmd.substring(30, 32);

			}else if(GatewaytConstant.ID_TYPE_FOUR_CASE_NORMAL_SWITCH_STR.equals(deviceId)){
				way2 = upStreamCmd.substring(24, 26);
				deviceStatus2 = upStreamCmd.substring(26, 28);
				way3 = upStreamCmd.substring(28, 30);
				deviceStatus3 = upStreamCmd.substring(30, 32);

			}
        }

		String onlineStatus = upStreamCmd.substring(18, 20);

		String signal = upStreamCmd.substring(22, 24);

		BoxModel bm = new BoxModelDao(GatewayServer.getInstance().getContext()).getCurrentBox();
		if(GatewaytConstant.getDeviceTypeMap().containsKey(deviceId)){
			DeviceModel dm;
			DeviceModel dm2;
			DeviceModel dm3;
			DeviceModel dm4;

			DeviceModelDao dmd = new DeviceModelDao(GatewayServer.getInstance().getContext());
			dm = dmd.getByCpuIdDeviceIdDeviceNumber(bm.getBoxCpuId(),deviceId, Number,way);
			if(dm != null){
				dm.setDeviceStatus(deviceStatus);
				dm.setSignal(signal);
				dm.setOnlineStatus(onlineStatus);
				dm.setIfAnyone(ifAnyone);
				dmd.add(dm);
				
				dm = dmd.getByCpuIdDeviceIdDeviceNumber(bm.getBoxCpuId(),dm.getDeviceId(), dm.getDeviceNumber(),way);
				list.add(dm);
				
				//统计耗电量
				new DeviceModelHistoryDao(GatewayServer.getInstance().getContext()).addModelHisotry(dm);
			}

			if(!StringUtil.isNullOrEmpty(way2)){
				dm2 = dmd.getByCpuIdDeviceIdDeviceNumber(bm.getBoxCpuId(),deviceId, Number,way2);
				if(dm2 != null){
					dm2.setDeviceStatus(deviceStatus2);
					dm2.setSignal(signal);
					dm2.setOnlineStatus(onlineStatus);
					dm2.setIfAnyone(ifAnyone);
					dmd.add(dm2);

					dm2 = dmd.getByCpuIdDeviceIdDeviceNumber(bm.getBoxCpuId(),dm2.getDeviceId(), dm2.getDeviceNumber(),way2);
					list.add(dm2);

					//统计耗电量
					new DeviceModelHistoryDao(GatewayServer.getInstance().getContext()).addModelHisotry(dm2);
				}
			}

			if(!StringUtil.isNullOrEmpty(way3)){
				dm3 = dmd.getByCpuIdDeviceIdDeviceNumber(bm.getBoxCpuId(),deviceId, Number,way3);
				if(dm3 != null){
					dm3.setDeviceStatus(deviceStatus3);
					dm3.setSignal(signal);
					dm3.setOnlineStatus(onlineStatus);
					dm3.setIfAnyone(ifAnyone);
					dmd.add(dm3);

					dm3 = dmd.getByCpuIdDeviceIdDeviceNumber(bm.getBoxCpuId(),dm3.getDeviceId(), dm3.getDeviceNumber(),way3);
					list.add(dm3);

					//统计耗电量
					new DeviceModelHistoryDao(GatewayServer.getInstance().getContext()).addModelHisotry(dm3);
				}
			}

			if(!StringUtil.isNullOrEmpty(way4)){
				dm4 = dmd.getByCpuIdDeviceIdDeviceNumber(bm.getBoxCpuId(),deviceId, Number,way4);
				if(dm4 != null){
					dm4.setDeviceStatus(deviceStatus4);
					dm4.setSignal(signal);
					dm4.setOnlineStatus(onlineStatus);
					dm4.setIfAnyone(ifAnyone);
					dmd.add(dm4);

					dm4 = dmd.getByCpuIdDeviceIdDeviceNumber(bm.getBoxCpuId(),dm4.getDeviceId(), dm4.getDeviceNumber(),way4);
					list.add(dm4);

					//统计耗电量
					new DeviceModelHistoryDao(GatewayServer.getInstance().getContext()).addModelHisotry(dm4);
				}
			}

		}

		updateData();
	}

	private void updateData(){
		BoxModel bm = new BoxModelDao(GatewayServer.getInstance().getContext()).getCurrentBox();
		//构造反馈数据
		DeviceStatusReply deviceStatusReply = new DeviceStatusReply();
		deviceStatusReply.setCurrentBoxCpuId(bm.getBoxCpuId());
		deviceStatusReply.setUserName(new BoxModelDao(GatewayServer.getInstance().getContext()).getCurrentBox().getUserName());
		deviceStatusReply.setDeviceStatus(list);

		JSONObject jsonObject = (JSONObject)JSON.toJSON(deviceStatusReply);
		list.clear();

		//发送所有当前手机
		UserServer.getInstance().sendAllUsersMessage(jsonObject.toJSONString());

		if("false".equals(jsonObject.getString(MsgConstant.HEAD_IS_REPLY))){
			jsonObject.put(MsgConstant.HEAD_IS_REPLY,"true");
			//发送服务器
			RemoteClient.getInstance().sendMessage(jsonObject.toJSONString());
			//发送其他机顶盒
			BoxServer.getInstance().sendAllBoxMessage(jsonObject.toJSONString());
		}
	}
}
